package me.magicall.web.simple.spring.controllers;

import me.magicall.db.model.BaseModel.MapModel;
import me.magicall.db.outsea.springjdbc.GetOne;
import me.magicall.web.simple.spring.util.U;
import me.magicall.web.simple.spring.util.UrlPatternRule;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * url：/模块名/id 或 /模块名/one/id
 * 
 * @author MaGiCalL
 */
@Controller("oneController")
@RequestMapping(method = RequestMethod.GET, //
value = { UrlPatternRule.SINGULAR_MODEL_NAME + "/" + UrlPatternRule.ID,//
		UrlPatternRule.SINGULAR_MODEL_NAME + "/one" + UrlPatternRule.ID, })
public class GetOneController {

//	@Autowired
//	protected MapModelAccessor mapModelAccessor;
	@Autowired
	private GetOne<MapModel<?>> getOne;

	@RequestMapping
	public String one(final ModelMap modelMap,//
			@PathVariable("modelName") final String modelName,//
			@PathVariable("id") final long id) {
		final MapModel<?> bean = U.getById(modelName, id, getOne);//mapModelAccessor.getOne(modelName, id, withReferenced(), withReferencing());
		if (id != 0) {
			modelMap.addAttribute("id", id);
		}
		U.setModelName(modelMap, modelName);
		U.setOne(modelMap, bean);
		return modelName;
	}

	protected boolean withReferenced() {
		return false;
	}

	protected boolean withReferencing() {
		return false;
	}
}
